package com.iot.service;

import com.iot.mapper.*;
import com.iot.pojo.MRef;
import com.iot.pojo.MTreeNode;
import com.iot.pojo.MTwork;
import com.iot.pojo.MTwork_zd;
import com.iot.util.Common;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service

public class TworkServiceImpl implements TworkService {
    @Autowired
    private TworkMapper mapper;
    @Autowired
    private DTworkMapper dmapper;
    @Autowired
    private Twork_zdMapper zdmapper;
    @Autowired
    private DRefMapper dRefMapper;
    @Autowired
    private DTreeMapper dTreeMapper;

    @Override
    public int getRecordCount(String sWhere) {
        return mapper.getRecordCount(sWhere);
    }

    @Override
    public int insert(MTwork mTwork) {
        return mapper.insert(mTwork);
    }

    @Override
    public void update(MTwork mTwork) {
        mTwork.setWORK_NO(Common.getWorkNo());
        mapper.update(mTwork);
    }

    @Override
    public void deleteById(int id) {
        mapper.deleteById(id);
    }

    @Override
    public MTwork single(Integer id) {
        return mapper.single(id);
    }

    @Override
    public List<MTwork> fetch(String sWhere) {
        return mapper.fetch(sWhere);
    }

    @Override
    public List<Map<String, Object>> fetchByPage(Map<String, Object> map) throws Exception {
        try {
            return dmapper.fetchByPage(map);
        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
    }


    @Override
    public List<Map<String, Object>> getWorkByEmpId(Map<String, Object> map) throws Exception {
        try {
        return dmapper.getWorkByEmpId(map);
        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
    }


    @Override
    public List<Map<String, Object>> getWorkByWorkId(Map<String, Object> map) {
        return dmapper.getWorkByWorkId(map);
    }

    @Override
    public void saveWorkZd(Map<String, Object> map) {
        //保存转单申请，就是将工单状态设置为 申请转单。
        Integer id = Common.toInt(map.get("workId"));
        //转单原因
        String strRes = map.get("handleDesc").toString();

        MTwork mTwork = mapper.single(id);
        mTwork.setWORK_STATUS_ID(2);
        mTwork.setHANDLE_DESC(strRes);
        mapper.update(mTwork);

    }

    @Override
    public void saveWorkJd(Map<String, Object> map) throws Exception {
        //维修工人结单
        Integer id = Common.toInt(map.get("workId"));
        //处理结果
        String strRes = map.get("handleDesc").toString();

        if (strRes.isEmpty() || strRes.trim() == "") {
            throw new Exception("处理结果必填！");
        }

        //上传图片返回的路径信息，例如 ：***.jpg,****.jpg
        String imgStr = map.get("imgStr").toString();
        MTwork mTwork = mapper.single(id);
        mTwork.setHANDLE_DESC(strRes);
        mTwork.setIMGSTR(imgStr);
        mTwork.setWORK_STATUS_ID(3);
        mapper.update(mTwork);

    }

    @Override
    public void SaveWorkSP(Map<String, Object> map) throws Exception {
        //正常结单 与转单申请 进行审批

        Integer id = Common.toInt(map.get("workId"));
        //
        String strRes = map.get("resDesc").toString();
//工单状态
        Integer workStatusId = Common.toInt(map.get("workStatusId"));
        //查询旧的工单信息，通过就工单信息
        MTwork mTwork = mapper.single(id);
//        //转单申请
//        if (mTwork.getWORK_STATUS_ID()==4){
//
//        }
        mTwork.setHANDLE_DESC(mTwork.getHANDLE_DESC() + " 确认结果：" + strRes);
        mTwork.setWORK_STATUS_ID(workStatusId);
        mapper.update(mTwork);
    }

   @Transactional(rollbackFor = Throwable.class)
    @Override
    public void SaveWorkZdSP(Map<String, Object> map) throws Exception {
        //转单申请 进行审批

        Integer id = Common.toInt(map.get("workId"));
        //
        String strRes = map.get("resDesc").toString();
        //
        String sdate = map.get("sdate").toString();


        Integer deptId = Common.toInt(map.get("deptId"));
        Integer empId = Common.toInt(map.get("empId"));
        Integer isOk= Common.toInt(map.get("isOk"));
        int appId = Common.toInt(map.get("appId"));
        //查询旧的工单信息，通过就工单信息
        MTwork mOTwork = mapper.single(id);
        Integer statusId=1;
        if (isOk==1) {
            //审批同意
            statusId=4;
            MTwork mNTwork = new MTwork();
            MTwork_zd mzd = new MTwork_zd();

            mNTwork.setWORK_STATUS_ID(1);
            mNTwork.setTITLE(mOTwork.getTITLE());
            mNTwork.setSDATE(Common.toshortDate(Common.getNowDate()));
            mNTwork.setWORK_NO(Common.getWorkNo());
            mNTwork.setCONTENT(mOTwork.getCONTENT());
            mNTwork.setWORK_TYPE_ID(mOTwork.getWORK_TYPE_ID());
            mNTwork.setDEPT_ID(deptId);
            mNTwork.setEMP_ID(empId);
            mNTwork.setDEVICE_ID(mOTwork.getDEVICE_ID());
            mapper.insert(mNTwork);
//        //转单申请

            if (mOTwork.getWORK_STATUS_ID() == 2) {
               // mzd.setSDATE(Common.toshortDate(Common.getNowDate()));
                mzd.setSDATE(mNTwork.getSDATE());
                mzd.setNWORK_ID(mNTwork.getWORK_ID());
                mzd.setOWORK_ID(mOTwork.getWORK_ID());
                mzd.setAPP_ID(appId);
                zdmapper.insert(mzd);
                // throw new Exception("像失败");
            }
        }
        mOTwork.setRES_DESC(" 检查结果：" + strRes);
        mOTwork.setWORK_STATUS_ID(statusId);
        mapper.update(mOTwork);



    }



    @Override
    public Map<String, Object> initList(int appId) {
        Map<String, Object> map = new HashMap<>();
        List<MTreeNode> deptList = dTreeMapper.getDept(appId);
        List<MRef> list = dRefMapper.getDeviceType();
        map.put("devicetype", list);
        List<MRef> workTypeList = dRefMapper.getWorkType();
        map.put("worktype", workTypeList);
        List<MRef> workStatusList = dRefMapper.getWorkStatus();
        map.put("workstatus", workStatusList);
        map.put("dept", deptList);
        return map;
    }

    @Override
    public Map<String, Object> initEdit(int appId) {
        Map<String, Object> map = new HashMap<>();
        List<MTreeNode> deptList = dTreeMapper.getDept(appId);
        List<MRef> list = dRefMapper.getDeviceType();
        map.put("devicetype", list);
        List<MRef> workTypeList = dRefMapper.getWorkType();
        map.put("worktype", workTypeList);
        List<MRef> postList = dRefMapper.getPost(appId);
        map.put("post", postList);
        map.put("dept", deptList);

        List<MRef> statusList = dRefMapper.getDeviceStatus();
        map.put("status", statusList);
        return map;
    }


}
